package cn.sumpu.app.coolsk.biz.impl;


import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

//import org.springframework.transaction.annotation.Transactional;

import cn.sumpu.app.coolsk.biz.CompanyBiz;
import cn.sumpu.app.coolsk.biz.ConfigBiz;
import cn.sumpu.app.coolsk.biz.HotelRoomBiz;
import cn.sumpu.app.coolsk.biz.MemberBiz;
import cn.sumpu.app.coolsk.biz.OrderBiz;
import cn.sumpu.app.coolsk.biz.ProductBiz;
import cn.sumpu.app.coolsk.biz.SmsBiz;
import cn.sumpu.app.coolsk.biz.TemplateBiz;
import cn.sumpu.app.coolsk.biz.TicketBiz;
import cn.sumpu.app.coolsk.constant.Constant;
import cn.sumpu.app.coolsk.constant.OrderPayFrom;
import cn.sumpu.app.coolsk.constant.OrderStatus;
import cn.sumpu.app.coolsk.constant.ProType;
import cn.sumpu.app.coolsk.constant.ProductType;
import cn.sumpu.app.coolsk.constant.Table;
import cn.sumpu.app.coolsk.dao.OrderDao;
import cn.sumpu.app.coolsk.domain.AdminUser;
import cn.sumpu.app.coolsk.domain.Company;
import cn.sumpu.app.coolsk.domain.Hotel;
import cn.sumpu.app.coolsk.domain.Member;
import cn.sumpu.app.coolsk.domain.Order;
import cn.sumpu.app.coolsk.domain.OrderLog;
import cn.sumpu.app.coolsk.domain.OrderProduct;
import cn.sumpu.app.coolsk.domain.Product;
import cn.sumpu.app.coolsk.domain.ProductPrice;
import cn.sumpu.app.coolsk.domain.Room;
import cn.sumpu.app.coolsk.domain.RoomPrice;
import cn.sumpu.app.coolsk.domain.Template;
import cn.sumpu.app.coolsk.domain.Ticket;
import cn.sumpu.app.coolsk.domain.TicketPrice;
import cn.sumpu.app.coolsk.exception.AppBusinessException;
import cn.sumpu.app.coolsk.util.WebUtil;
import cn.sumpu.toolkit.common.page.PageModel;
import cn.sumpu.toolkit.common.util.Common;

public class OrderBizImpl extends QuartzJobBean implements OrderBiz,InitializingBean{

	private OrderDao orderDao;

	private DataSourceTransactionManager transactionManager;
	@Autowired
	private IdentifierBizImpl identifierBiz;
	@Autowired
	private SmsBiz smsBiz;
	@Autowired
	private TemplateBiz templateBiz;
	@Autowired
	private TicketBiz ticketBiz;
	@Autowired
	private MemberBiz memberBiz;
	@Autowired
	private HotelRoomBiz hotelRoomBiz;
	@Autowired
	private ConfigBiz configBiz;
	@Autowired
	private ProductBiz productBiz;
	@Autowired
	private CompanyBiz companyBiz;
	
	private Scheduler scheduler;
	public void setScheduler(Scheduler scheduler) {
		this.scheduler = scheduler;
	}

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void setTransactionManager(
			DataSourceTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	
	@Override
	public Order saveOrder(Order order,Product product, Integer count, Member member)throws AppBusinessException {
		if(member == null || product == null ||  order == null || count==null || count < 1)return null;
		String orderId = identifierBiz.generateId(Table.ORDER); 
		order.setMember(member.getId()); 
		order.setPayFrom(product.getPayFrom());
		order.setStatus(OrderStatus.TOPAID.getValue());
		order.setId(orderId);
		order.setPname(product.getName());
		order.setType(ProductType.PRODUCT.getValue()); 
		order.setCompany(product.getCompany());
		if(Common.empty(order.getOrderTime())){
			order.setOrderTime(Common.getFormatDate(new Date(), "yyyy-MM-dd"));
		}else{
			ProductPrice productPrice=productBiz.getDayPrice(product, order.getOrderTime());
			product.setPriceMin(productPrice.getPriceA());
		}
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 设置事务传播属性
		TransactionStatus status = transactionManager.getTransaction(def);// 返回事务对象

		try{
			order.setTotalFee(product.getPriceMin() * count);
//			保存订单
			orderDao.insertOrder(order);
			OrderProduct op = new OrderProduct();
			op.setAmount(count);
			op.setMprice(product.getPriceMarket());
			op.setNprice(product.getPriceMin());
			op.setOid(orderId);
			op.setPid(product.getId());
			op.setPname(product.getName());
			if(Common.empty(order.getOrderTime())){
				op.setSdate(Common.getFormatDate(new Date(), "yyyy-MM-dd"));
			}else{
				op.setSdate(order.getOrderTime());
			}
			
			op.setType(ProductType.PRODUCT.getValue());
//			保存订单产品
			orderDao.insertOrderProduct(op);
//          保存订单日志
			String  memo="订单内包含"+op.getPname()+"产品。"+"数量:"+op.getAmount();
			saveOrderLogBypayOrder(order,member,memo);
			transactionManager.commit(status);// 提交事务
//			订单加入定时任务,60分钟后如果未付款自动取消
			orderJob(order, 60, 3);
			/*
			Template template = templateBiz.getTemplateById(Constant.TEMP_TO_PAID);
			if(template != null){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("orderId", order.getId());
				map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
		//		String content = getSmsContent(template.getSource(),map);
//				发送短信
				boolean send = smsBiz.send(content, order.getPhone());
				if(!send){ 
					throw new Exception("send sms failure!");
				}
			}
			
		 	*/
			
		}catch(Exception e){
			e.printStackTrace();
			transactionManager.rollback(status);// 回滚事务
			throw new AppBusinessException(e);
		}
		

		
		return order;
	}

	@Override
	public Order saveOrder(Order order,Ticket ticket, TicketPrice tprice,Integer count,Integer countC, Member member)throws AppBusinessException {
		if(member == null || ticket == null || tprice == null || order == null || count==null || count < 1)return null;
		String orderId = identifierBiz.generateId(Table.ORDER);
		order.setMember(member.getId()); 
		order.setPayFrom(ticket.getPayFrom());
		order.setStatus(OrderStatus.TOPAID.getValue());
		order.setId(orderId);
		order.setPname(ticket.getName());
		order.setType(ProductType.TICKET.getValue()); 
		order.setCompany(ticket.getCompany());
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 设置事务传播属性
		TransactionStatus status = transactionManager.getTransaction(def);// 返回事务对象

		try{
			double totalFee = tprice.getPriceA() * count;
			

			OrderProduct op = new OrderProduct();
			op.setAmount(count);
			op.setMprice(ticket.getPriceMarket());
			op.setNprice(tprice.getPriceA());
			op.setOid(orderId);
			op.setPid(ticket.getId());
			op.setPname(ticket.getName());
			op.setSdate(tprice.getSpDate());
			op.setType(ProductType.TICKET.getValue());
//			保存成人门票订单产品
			orderDao.insertOrderProduct(op);
			String memoBaby="";
			String  memo="订单内包含"+op.getPname()+"产品。"+"数量"+op.getAmount()+memoBaby;
//记录日记
			if(countC != null && countC > 0 && tprice.getPriceC() > 0){
				op.setAmount(countC);
				op.setMprice(ticket.getPriceMarket());
				op.setNprice(tprice.getPriceC());
				op.setOid(orderId);
				op.setPid(ticket.getId());
				op.setPname(ticket.getName()+"(儿童票)");
				op.setSdate(tprice.getSpDate());
				op.setType(ProductType.TICKET.getValue());
//				保存儿童门票订单产品
				orderDao.insertOrderProduct(op);
				memoBaby=op.getPname()+"产品。"+"数量"+op.getAmount();
				totalFee = totalFee+tprice.getPriceC() * countC;
			}
			order.setTotalFee(totalFee);
//			保存订单
			orderDao.insertOrder(order);
//          保存订单日志
			
			saveOrderLogBypayOrder(order,member,memo);
			transactionManager.commit(status);// 提交事务
//			订单加入定时任务,60分钟后如果未付款自动取消
			orderJob(order, 60, 3);
			/*
			if(ProductType.TICKET.getValue().equals(order.getType())){
				Template template = templateBiz.getTemplateById(Constant.TEMP_TO_PAID);
				if(template != null){
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("orderId", order.getId());
					map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
					String content = getSmsContent(template.getSource(),map);
//					发送短信
					boolean send = smsBiz.send(content, order.getPhone());
					if(!send){ 
						throw new Exception("send sms failure!");
					}
				}
			}
			
			*/

			
		}catch(Exception e){
			e.printStackTrace();
			transactionManager.rollback(status);// 回滚事务
			throw new AppBusinessException(e);
		}
		

		
		return order;
	}
	@Override
	public Order saveOrder(Order order,Room room, RoomPrice tprice,Integer count, Member member,String ldate)throws AppBusinessException {
		if(member == null || room == null || tprice == null || order == null || count==null || count < 1)return null;
		String orderId = identifierBiz.generateId(Table.ORDER);
		Hotel hotel=hotelRoomBiz.getHotelById(room.getHotel());
		order.setMember(member.getId()); 
		order.setPayFrom(hotel.getPayFrom());
		order.setStatus(OrderStatus.TOPAID.getValue());
		order.setId(orderId);
		order.setPname(hotel.getName()+"("+room.getName()+")");
		order.setType(ProductType.HOTEL.getValue()); 
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
		def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 设置事务传播属性
		TransactionStatus status = transactionManager.getTransaction(def);// 返回事务对象
		try{
			int day=Common.daysOfTwo(Common.parseDate(tprice.getSpDate(), "yyyy-MM-dd"), Common.parseDate(ldate, "yyyy-MM-dd"));
			Double money=tprice.getPriceA()*count*day;
			order.setTotalFee(money);
//			保存订单
			orderDao.insertOrder(order); 
			OrderProduct op = new OrderProduct();
			op.setAmount(count);
			op.setMprice(room.getPriceMarket());
			op.setNprice(tprice.getPriceA());
			op.setOid(orderId);
			op.setPid(room.getId());
			op.setPname(hotel.getName()+"("+room.getName()+")");
			op.setSdate(tprice.getSpDate());
			op.setType(ProductType.HOTEL.getValue());
			op.setLdate(ldate);
//			保存订单产品
			orderDao.insertOrderProduct(op);
//          保存订单日志
			String  memo="订单内包含"+op.getPname()+"产品。"+"数量"+op.getAmount();
			saveOrderLogBypayOrder(order,member,memo);
			transactionManager.commit(status);// 提交事务
//			订单加入定时任务,60分钟后如果未付款自动取消
			orderJob(order, 60, 3);
			/*
			 * 酒店预定时不发送短信
			 * */
			/*Template template = templateBiz.getTemplateById(Constant.TEMP_HOTEL);
			if(template != null){
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("orderId", order.getId());
				map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
				String content = getSmsContent(template.getSource(),map);
//				发送短信
				boolean send = smsBiz.send(content, order.getPhone());
				if(!send){ 
					throw new Exception("send sms failure!");
				}
			}*/
		}catch(Exception e){
			e.printStackTrace();
			transactionManager.rollback(status);// 回滚事务
			throw new AppBusinessException(e);
		}
		

		
		return order;
	}
	@Override
	public Order getOrderById(String id) throws AppBusinessException {
		if(Common.empty(id)) return null;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		List<Order> orderList = orderDao.getOrder(map);
		if(orderList != null && orderList.size() > 0){
			return orderList.get(0);
		}
		return null;
	}

	@Override
	public List<OrderProduct> getOpList(String oid) throws AppBusinessException {
		if(Common.empty(oid)) return null; 
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("oid", oid);
		return orderDao.getOrderProduct(map);
	}

	@Override
	public void payOrderSuc(Order order) throws AppBusinessException {
		if(order == null) return ;
		if(OrderStatus.TOPAID.getValue().equals(order.getStatus()) && OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
			DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
			def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 设置事务传播属性
			TransactionStatus status = transactionManager.getTransaction(def);// 返回事务对象
			try{
				order.setStatus(OrderStatus.PAIDED.getValue());
				orderDao.updateOrder(order);
//		          保存订单日志
				String  memo="系统自动已完成付款记录";
				Member member =new Member();
				member.setNickname("*系统*");
				saveOrderLogBypayOrder(order,member,memo);
				
				transactionManager.commit(status);// 提交事务
				
				if(ProductType.TICKET.getValue().equals(order.getType())){
					Template template = templateBiz.getTemplateById(Constant.SCENIC_ONLINEPAY_SUC);
					if(template != null){
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("orderId", order.getId());
						map.put("orderTime", order.getOrderTime());
						map.put("pname", order.getPname());
						/*Ticket ticket = getTicketByOrder(order.getId());
						if(ticket != null){
							map.put("address", ticket.getAddress());
						}*/
						map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
						String content = getSmsContent(template.getSource(),map);
//							发送短信
						boolean send = smsBiz.send(content, order.getPhone());
						if(!send){
							throw new Exception("send sms failure!");
						}

					}
				}else if(ProductType.HOTEL.getValue().equals(order.getType())){
					Template template = templateBiz.getTemplateById(Constant.HOTEL_ONLINEPAY_SUC);
					if(template != null){
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("orderId", order.getId());
						map.put("orderTime", Common.getFormatDate(Common.parseDate(order.getOrderTime(), "yyyy-MM-dd"), "MM/dd"));
						map.put("orderCount", order.getOpList().get(0).getAmount());
						map.put("price", order.getOpList().get(0).getNprice());
						Room room = hotelRoomBiz.getRoomById(order.getOpList().get(0).getPid());
						if(room != null){
							map.put("pname", room.getName());
							map.put("pdates", Common.daysOfTwo(Common.parseDate(order.getOpList().get(0).getSdate(), "yyyy-MM-dd"), Common.parseDate(order.getOpList().get(0).getLdate(), "yyyy-MM-dd")));
							Hotel hotel = hotelRoomBiz.getHotelById(room.getHotel());
							if(hotel != null){
								map.put("address", hotel.getAddress());
								map.put("phone", hotel.getPhone());
								map.put("hotelname", hotel.getName());
							}
							map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
							String content = getSmsContent(template.getSource(),map);
//								发送短信
							boolean send = smsBiz.send(content, order.getPhone());
							if(!send){
								throw new Exception("send sms failure!");
							}
						}
					}
				}else if(ProductType.PRODUCT.getValue().equals(order.getType())){
					Template template = null;
					if(order.getOpList()!=null && order.getOpList().size() > 0){
						Product p = productBiz.getProductById(order.getOpList().get(0).getPid());
						if(ProType.FOOD.getValue().equals(p.getType())){
							template = templateBiz.getTemplateById(Constant.FOOD_ONLINEPAY_SUC);
						}else if(ProType.TRAVEL.getValue().equals(p.getType())){
							template = templateBiz.getTemplateById(Constant.TRIP_ONLINEPAY_SUC);
						}else if(ProType.LINE.getValue().equals(p.getType())){
							template = templateBiz.getTemplateById(Constant.LINE_ONLINEPAY_SUC);
						}
						
						if(template != null){
							Map<String,Object> map = new HashMap<String,Object>();
							map.put("orderId", order.getId());
							map.put("orderId", order.getPname());
							map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
							String content = getSmsContent(template.getSource(),map);

//							发送短信
							boolean send = smsBiz.send(content, order.getPhone());
							if(!send){
								throw new Exception("send sms failure!");
							}
						}
					}
					
				}
				
				
				
			}catch(Exception e){
				transactionManager.rollback(status);// 回滚事务
				throw new AppBusinessException(e);
			}
		}
	}
	
	public Ticket getTicketByOrder(String orderId)throws AppBusinessException{
		List<OrderProduct> opList = getOpList(orderId);
		if(opList != null && opList.size() > 0){
			Ticket ticket = ticketBiz.getTicketById(opList.get(0).getPid());
			return ticket;
		}
		return null;
	}
	
	public String getSmsContent(String template, Map<String,Object> data) throws AppBusinessException{
		  String regex = "\\$\\{(.+?)\\}";  
	        Pattern pattern = Pattern.compile(regex);  
	        Matcher matcher = pattern.matcher(template);  
	        /* 
	         * sb用来存储替换过的内容，它会把多次处理过的字符串按源字符串序 
	         * 存储起来。 
	         */  
	        StringBuffer sb = new StringBuffer();  
	        while (matcher.find()) {  
	            String name = matcher.group(1);//键名  
	            String value = String.valueOf(data.get(name));//键值  
	            if (value == null) {  
	                value = "";  
	            } else {  
	                /* 
	                 * 由于$出现在replacement中时，表示对捕获组的反向引用，所以要对上面替换内容 
	                 * 中的 $ 进行替换，让它们变成 "\$1000.00" 或 "\$1000000000.00" ，这样 
	                 * 在下面使用 matcher.appendReplacement(sb, value) 进行替换时就不会把 
	                 * $1 看成是对组的反向引用了，否则会使用子匹配项值amount 或 balance替换 $1 
	                 * ，最后会得到错误结果： 
	                 * 
	                 * 尊敬的客户刘明你好！本次消费金额amount000.00，您帐户888888888上的余额 
	                 * 为balance000000.00，欢迎下次光临！ 
	                 * 
	                 * 要把 $ 替换成 \$ ，则要使用 \\\\\\& 来替换，因为一个 \ 要使用 \\\ 来进 
	                 * 行替换，而一个 $ 要使用 \\$ 来进行替换，因 \ 与  $ 在作为替换内容时都属于 
	                 * 特殊字符：$ 字符表示反向引用组，而 \ 字符又是用来转义 $ 字符的。 
	                 */  
	                value = value.replaceAll("\\$", "\\\\\\$");  
	                //System.out.println("value=" + value);  
	            }  
	            /* 
	             * 经过上面的替换操作，现在的 value 中含有 $ 特殊字符的内容被换成了"\$1000.00" 
	             * 或 "\$1000000000.00" 了，最后得到下正确的结果： 
	             * 
	             * 尊敬的客户刘明你好！本次消费金额$1000.00，您帐户888888888上的 
	             * 余额为$1000000.00，欢迎下次光临！ 
	             * 
	             * 另外，我们在这里使用Matcher对象的appendReplacement()方法来进行替换操作，而 
	             * 不是使用String对象的replaceAll()或replaceFirst()方法来进行替换操作，因为 
	             * 它们都能只能进行一次性简单的替换操作，而且只能替换成一样的内容，而这里则是要求每 
	             * 一个匹配式的替换值都不同，所以就只能在循环里使用appendReplacement方式来进行逐 
	             * 个替换了。 
	             */  
	            matcher.appendReplacement(sb, value);  
	        }  
	        //最后还得要把尾串接到已替换的内容后面去，这里尾串为“，欢迎下次光临！”  
	        matcher.appendTail(sb);  
	        return sb.toString();  
	}

	@Override
	public PageModel getOrderList(Integer page, Integer pagesize, Order order)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		int size = pagesize;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		
		map.put("id", order.getId()); 
		map.put("pname", order.getPname());
		
		map.put("phone", order.getPhone());
		if(!Common.empty(order.getStatus())){
			map.put("status", order.getStatus());
		}
		if(!Common.empty(order.getCompany())){
			map.put("company", order.getCompany());
		}
		if(!Common.empty(order.getContactor())){
			map.put("contactor", order.getContactor());
		}if(!Common.empty(order.getType())){
			map.put("type", order.getType());
		}if(!Common.empty(order.getStartTime())){
			map.put("startTime", order.getStartTime());
		}if(!Common.empty(order.getEndTime())){
			map.put("endTime", order.getEndTime());
		}
		
		
		int count = orderDao.getOrderCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<Order> orderList = orderDao.getOrder(map);
		//  获得订单产品的id 
		for(Order orderOne :orderList){ 
			Map<String, Object> mapProduct = new HashMap<String, Object>();
			mapProduct.put("oid", orderOne.getId());
			List<OrderProduct> orderProductList =orderDao.getOrderProduct(mapProduct);
			if(orderProductList.size()>0){
				orderOne.setPid(orderProductList.get(0).getPid());
			}
		}
		pm.setList(orderList);
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}

	/*@Override
	public boolean audiSuc(String orderId) throws AppBusinessException {
		if(Common.empty(orderId)) return false;
		Order order = getOrderById(orderId);
		if(order != null){
			order.setId(orderId);
			order.setStatus(OrderStatus.BILLED.getValue());
			String stampNum = WebUtil.randTicketNum(new String[]{});
			order.setStampNum(stampNum);
			
			try{
				orderDao.updateOrder(order);
				
				if(ProductType.TICKET.getValue().equals(order.getType())){
					Template template = templateBiz.getTemplateById(Constant.TEMP_BILLED);
					if(template != null){
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("pname", order.getPname());
						map.put("orderRandom", stampNum);
						Ticket ticket = getTicketByOrder(order.getId());
						if(ticket != null){
							map.put("address", ticket.getAddress());
						}
						map.put("contactor", order.getContactor());
						map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
						String content = getSmsContent(template.getSource(),map);
						if(!ProductType.PRODUCT.getValue().equals(order.getType())){
//							发送短信
							boolean send = smsBiz.send(content, order.getPhone());
							if(!send){
								throw new Exception("send sms failure!");
							}
						}

					}
				}else if(ProductType.HOTEL.getValue().equals(order.getType())){
					Template template = templateBiz.getTemplateById(Constant.TEMP_HOTEL_CONFIRM);
					if(template != null){
						Map<String,Object> map = new HashMap<String,Object>();
						map.put("orderId", order.getId());
						map.put("orderTime", Common.getFormatDate(Common.parseDate(order.getOrderTime(), "yyyy-MM-dd"), "MM/dd"));
						map.put("orderCount", order.getOpList().get(0).getAmount());
						
						map.put("price", order.getOpList().get(0).getMprice());
						Room room = hotelRoomBiz.getRoomById(order.getOpList().get(0).getPid());
						if(room != null){
							map.put("pname", room.getName());
							map.put("pdates", Common.daysOfTwo(Common.parseDate(order.getOpList().get(0).getSdate(), "yyyy-MM-dd"), Common.parseDate(order.getOpList().get(0).getLdate(), "yyyy-MM-dd")));
							Hotel hotel = hotelRoomBiz.getHotelById(room.getHotel());
							if(hotel != null){
								map.put("address", hotel.getAddress());
								map.put("phone", hotel.getPhone());
								map.put("hotelname", hotel.getName());
							}
							map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
							String content = getSmsContent(template.getSource(),map);
//								发送短信
							boolean send = smsBiz.send(content, order.getPhone());
							if(!send){
								throw new Exception("send sms failure!");
							}
						}
						

					}
				}
				
				
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
			return true;
		}
		
		return false;
	}*/
	
	
	
	
@Override
	public void afterPropertiesSet() throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("status", OrderStatus.TOPAID.getValue());
		map.put("payFrom", OrderPayFrom.INTERNET.getValue());
		List<Order> orderList = orderDao.getOrder(map);
		for (Order order : orderList) {
//			60分钟
			orderJob(order, 60, 3);
		}
	}

	//	如果订单规定时间内未支付将自动取消该订单
	public void orderJob(Order order,int numbs,int type) throws SchedulerException{
		if(order == null || !order.getStatus().equals(OrderStatus.TOPAID.getValue())){
			return ; 
		}else if (scheduler.getJobDetail("order" + order.getId(), null) != null) {
			scheduler.deleteJob("order" + order.getId(),null);
		}
		JobDetail job = new JobDetail("order" + order.getId(), null, this.getClass());
		// 预约的内容
		job.getJobDataMap().put("order", order);
		job.getJobDataMap().put("orderDao", orderDao);
		job.getJobDataMap().put("orderBiz", this);
		Date date = order.getGmtCreated();
		switch(type){
			case 0:break;
			case 1:{
				date = Common.addDate(date, numbs);
				break;
			}
			case 2:{
				date = Common.addHours(date, numbs);
				break;
			}
			case 3:{
				date = Common.addMinutes(date, numbs);
				break;
			}
			default:break;
		}
		Trigger trigger = new SimpleTrigger("order" + order.getId(), null, date);
		scheduler.scheduleJob(job, trigger);
	}

	@Override
	protected void executeInternal(JobExecutionContext context)
			throws JobExecutionException {
		synchronized(OrderBizImpl.class){
			try { 
				JobDataMap dataMap = context.getJobDetail().getJobDataMap(); 
				// 取得预约内容
				Order order = (Order) dataMap.get("order");
				OrderBiz orderBiz = (OrderBiz) dataMap.get("orderBiz");
				Order ordered = orderBiz.getOrderById(order.getId());
				// 如果该预约内容已经被删除就不执行了
				if (ordered == null) {
					return;
				}
				
				// 如果订单状态为未支付且订单支付方式为线上支付
				if(ordered.getStatus().equals(OrderStatus.TOPAID.getValue()) && ordered.getPayFrom().equals(OrderPayFrom.INTERNET.getValue())){
					OrderDao orderDao = (OrderDao) dataMap.get("orderDao");
					ordered.setStatus(OrderStatus.CANCEL.getValue());
					orderDao.updateOrder(ordered);
					//记录日志
					Member member =new Member();
					member.setNickname("系统/超时操作");
					String memo="超过1小时未支付，系统自动取消订单。";
					saveOrderLogBypayOrder(ordered,member,memo);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 查询我家的订单
	 */
	@Override
	public PageModel getOrderListByMemberId(Integer page, Integer pagesize, String memberId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		int size = pagesize;
		map.put("startId", (page - 1) * size);
		map.put("pageSize", size);
		map.put("member", memberId);
		int count = orderDao.getOrderCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		List<Order> orderList = orderDao.getOrderListByMemberId(map);
		pm.setList(orderList);
		pm.setPageSize(size);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + size - 1) / size);
		return pm;
	}

	@Override
	public void saveOrder(Order order,Member member) throws AppBusinessException {
	    DefaultTransactionDefinition def = new DefaultTransactionDefinition();// 事务定义类
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);// 设置事务传播属性
        TransactionStatus status = transactionManager.getTransaction(def);// 返回事务对象
        try{
            orderDao.updateOrder(order);  
//            order.getp
//          保存订单日志
            String  memo="该订单已由用户手动取消";
            saveOrderLogBypayOrder(order,member,memo);
            transactionManager.commit(status);// 提交事务
        }catch(Exception e){
            transactionManager.rollback(status);// 回滚事务
            throw new AppBusinessException(e);
        }
		
	}
	//查看订单详情
	@Override
	public Order getOrderDtlById( String orderId) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", orderId);  
		Order order = orderDao.getOrderDtlById(map);
		List<OrderProduct> orderProductList =order.getOpList();
		for(OrderProduct op :orderProductList){
			String companyId=null;
			if(("01").equals(op.getType())){
				//门票
				Ticket ticket =ticketBiz.getTicketById(op.getPid());
				 companyId =ticket.getCompany();
				
			}else if(("02").equals(op.getType())){
				//酒店
				Room hotelRoom = hotelRoomBiz.getRoomById(op.getPid());
				 companyId =hotelRoom.getCompany();
				
			}else if(("03").equals(op.getType())){
				//产品
				Product product =productBiz.getProductById(op.getPid());
				companyId =product.getCompany(); 
				
			} 
			Company company = companyBiz.getCompanyById(companyId);
			if(company!=null && companyId!=null){
				op.setCompanyName(company.getName());
				op.setCompanyPhone(company.getPhone());
			}
			
		}
		
		Member member =memberBiz.getMemberById(order.getMember());
		order.setMemberObj(member);
		return order;
	}
	//更新订单
	
	@Override  
	public void updateOrder(Order order,AdminUser user,String reason)throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", order.getId());  
		Order orderOld =orderDao.getOrderDtlById(map);
		//记录订单日记
		OrderLog orderLog = new OrderLog();
		orderLog.setId(identifierBiz.generateId("coolsk_order_log"));
		orderLog.setOrderId(order.getId());
		orderLog.setPreStatus(orderOld.getStatus());
		orderLog.setStatus(order.getStatus()); 
		orderLog.setTrigger(user.getRealname()); 
		orderLog.setTriggerId(user.getId());
		String preStatus = chanelStatus(orderLog.getPreStatus());
		String status = chanelStatus(orderLog.getStatus());
		String memo ="操作原由："+reason+"。记录："+user.getRealname()+"将标识号为"+order.getId()+"的订单由原来订单状态:"+preStatus+"更改为当前:"+status+"。";
		orderLog.setMemo(memo);
		//更新订单
		orderDao.updateOrder(order);
		//记录
		orderDao.insertOrderLog(orderLog);
	}
	//取消挂起
	
	@Override   
	public void cancelOrderGQ(String id,AdminUser user,String reason)throws AppBusinessException {
		//查出挂起前的状态
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderId", id);  
		OrderLog orderLog =orderDao.selectOrderLogByid(map);
		//当前状态 
		String status =orderLog.getStatus();
		//上一步状态
		String prestatus =orderLog.getPreStatus();
		//更新订单状态
		Order order =new Order();
		order.setId(id);
		order.setStatus(prestatus);
		//记录日志
		OrderLog orderLogNew = new OrderLog();
		orderLogNew.setId(identifierBiz.generateId("coolsk_order_log"));
		orderLogNew.setOrderId(id);
		orderLogNew.setPreStatus(status);
		orderLogNew.setStatus(prestatus);
		orderLogNew.setTrigger(user.getRealname()); 
		orderLogNew.setTriggerId(user.getId());
		String preStatusNew = chanelStatus(orderLogNew.getPreStatus());
		String statusNew = chanelStatus(orderLogNew.getStatus());
		String memo ="挂起操作原由："+reason+"。记录："+user.getRealname()+"将标识号为"+id+"的订单由原来订单状态:"+preStatusNew+"更改为当前:"+statusNew+"。";
		orderLogNew.setMemo(memo);
		//更新订单
		orderDao.updateOrder(order);
		//记录
		orderDao.insertOrderLog(orderLogNew);
	}
	//下定单生成日志
	@Override   
	public void saveOrderLogBypayOrder(Order order,Member member,String memo)throws AppBusinessException {
		OrderLog orderLog = new OrderLog();
		orderLog.setId(identifierBiz.generateId("coolsk_order_log"));
		orderLog.setOrderId(order.getId());
		orderLog.setPreStatus(order.getStatus());
		orderLog.setStatus(order.getStatus());
		orderLog.setTrigger(member.getNickname()); 
		orderLog.setTriggerId(order.getMember());
		orderLog.setMemo(memo);
		//记录
		orderDao.insertOrderLog(orderLog);
	}
	//查询默认短信内容
	@Override 
	public List<Template> getTemplateList(String orderId)throws AppBusinessException { 
		Order order =getOrderById(orderId);
		if(order != null){
			order.setOpList(getOpList(orderId));
		}
		String searchName="";
		if((ProductType.TICKET.getValue()).equals(order.getType())){
			searchName="门票";
		}else if((ProductType.HOTEL.getValue()).equals(order.getType())){
			searchName="酒店";
		}else if((ProductType.PRODUCT.getValue()).equals(order.getType())){ 
			OrderProduct orderproduct =getOpList(order.getId()).get(0);
			Product product =productBiz.getProductById(orderproduct.getPid());
			if((ProType.FOOD.getValue()).equals(product.getType())){
				searchName="特价美食";
			}else if((ProType.TRAVEL.getValue()).equals(product.getType())){
				searchName="旅游商品";
			}else if((ProType.LINE.getValue()).equals(product.getType())){
				searchName="线路推荐";
			}
			
		}
		List<Template> templateList =templateBiz.getTemplateList(searchName);
		for(Template template :templateList){
				
				Map<String,Object> map = new HashMap<String,Object>();
				map.put("orderId", order.getId());
				map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
				//门票
				if(ProductType.TICKET.getValue().equals(order.getType())){
					map.put("orderTime", order.getOrderTime());
					map.put("pname", order.getPname());
					map.put("orderRandom", order.getStampNum());
					map.put("contactor", order.getContactor());
					Ticket ticket = getTicketByOrder(order.getId());
					if(ticket != null){
						map.put("address", ticket.getAddress());
					}
				//酒店
				}else if(ProductType.HOTEL.getValue().equals(order.getType())){
					try {
						map.put("orderTime", Common.getFormatDate(Common.parseDate(order.getOrderTime(), "yyyy-MM-dd"), "MM/dd"));
						map.put("orderCount", String.valueOf(order.getOpList().get(0).getAmount()));
						map.put("price", String.valueOf(order.getOpList().get(0).getNprice()));
						Room room = hotelRoomBiz.getRoomById(order.getOpList().get(0).getPid());
						if(room != null){
							map.put("pname", room.getName());
							map.put("pdates", String.valueOf(Common.daysOfTwo(Common.parseDate(order.getOpList().get(0).getSdate(), "yyyy-MM-dd"), Common.parseDate(order.getOpList().get(0).getLdate(), "yyyy-MM-dd"))));
							Hotel hotel = hotelRoomBiz.getHotelById(room.getHotel());
							if(hotel != null){
								map.put("address", hotel.getAddress());
								map.put("phone", hotel.getPhone());
								map.put("hotelname", hotel.getName());
							}
						}
					} catch (ParseException e) {
						throw new AppBusinessException("dateoftwo error", e);
					}
				}else if(ProductType.PRODUCT.getValue().equals(order.getType())){
						map.put("orderTime", order.getOrderTime());
						OrderProduct orderproduct =getOpList(order.getId()).get(0);
						Product product =productBiz.getProductById(orderproduct.getPid());
						if(product != null){
							map.put("address", product.getAddress());
							map.put("pname", product.getName());
						}
				}
				String content = getSmsContent(template.getSource(),map);
				template.setSource(content);
		}
		return templateList;
	}
	//查看日志
	@Override 
	public PageModel getOrderLogList(Integer page, Integer pagesize, String id) throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * pagesize);
		map.put("pageSize", pagesize);
		map.put("orderId", id);   
		int count = orderDao.getOrderLogCount(map); 
		PageModel pm = new PageModel(); 
		pm.setCurrentPage(page);
		List<OrderLog> orderLogList = orderDao.getOrderLogList(map);
		pm.setList(orderLogList);
		pm.setPageSize(pagesize);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + pagesize - 1) / pagesize);
		return pm;
	}
	//数值转换状态
	@Override  
	public String chanelStatus(String status) throws AppBusinessException {
		 
		if(("01").equals(status)){
			status="待支付";
		}else if(("02").equals(status)){
			status="已支付";
		}else if(("03").equals(status)){
			status="已成功";
		}else if(("04").equals(status)){
			status="已完成";
		}else if(("05").equals(status)){
			status="已取消";
		}else if(("06").equals(status)){
			status="挂起";
		}
		return status;
	}
	//确定订单(短信)
	
	@Override  
	public boolean defineOrder(String orderId)throws AppBusinessException {
		Order order =getOrderDtlById(orderId);
		if(order != null){
			order.setId(orderId);
			order.setStatus(OrderStatus.BILLED.getValue());
			String stampNum = WebUtil.randTicketNum(new String[]{});
			order.setStampNum(stampNum);
			try{ 
				orderDao.updateOrder(order);
				//门票
				if(ProductType.TICKET.getValue().equals(order.getType())){
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("orderId", order.getId());
					map.put("orderTime", Common.getFormatDate(Common.parseDate(order.getOrderTime(), "yyyy-MM-dd"), "MM/dd"));
					map.put("pname", order.getPname());
					map.put("orderRandom", stampNum);
					Ticket ticket = getTicketByOrder(order.getId());
					if(ticket != null){
						map.put("address", ticket.getAddress());
					}
					map.put("contactor", order.getContactor());
					map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
					//线上支付
					if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
						//确认短信
						Template template = templateBiz.getTemplateById(Constant.SCENIC_ONLINEPAY_DEFINE);
						if(template != null){
							String content = getSmsContent(template.getSource(),map);
							if(!ProductType.PRODUCT.getValue().equals(order.getType())){
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						}
						//到付
					}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
						//确认短信
						Template template = templateBiz.getTemplateById(Constant.SCENIC_TOPAY_DEFINE);
						if(template != null){
							String content = getSmsContent(template.getSource(),map);
							if(!ProductType.PRODUCT.getValue().equals(order.getType())){
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						}
					}
					//酒店
				}else if(ProductType.HOTEL.getValue().equals(order.getType())){
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("orderId", order.getId());
					map.put("orderTime", Common.getFormatDate(Common.parseDate(order.getOrderTime(), "yyyy-MM-dd"), "MM/dd"));
					map.put("orderCount", order.getOpList().get(0).getAmount());
					map.put("price", order.getOpList().get(0).getMprice());
					Room room = hotelRoomBiz.getRoomById(order.getOpList().get(0).getPid());
					if(room != null){
						map.put("pname", room.getName());
						map.put("pdates", Common.daysOfTwo(Common.parseDate(order.getOpList().get(0).getSdate(), "yyyy-MM-dd"), Common.parseDate(order.getOpList().get(0).getLdate(), "yyyy-MM-dd")));
						Hotel hotel = hotelRoomBiz.getHotelById(room.getHotel());
						if(hotel != null){
							map.put("address", hotel.getAddress());
							map.put("phone", hotel.getPhone());
							map.put("hotelname", hotel.getName());
						}
					}
						map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
					//线上支付
					if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
						
						Template template = templateBiz.getTemplateById(Constant.HOTEL_ONLINEPAY_DEFINE);
						if(template != null){
							
								String content = getSmsContent(template.getSource(),map);
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						
						//到付
					}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
						Template template = templateBiz.getTemplateById(Constant.HOTEL_TOPAY_DEFINE);
						if(template != null){
								String content = getSmsContent(template.getSource(),map);
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						}
					//产品
				}else if(ProductType.PRODUCT.getValue().equals(order.getType())){
					OrderProduct orderproduct =getOpList(order.getId()).get(0);
					Product product =productBiz.getProductById(orderproduct.getPid());
					Map<String,Object> map = new HashMap<String,Object>();
					if(product != null){
						map.put("address", product.getAddress());
						map.put("pname", product.getName());
						}
					map.put("orderId", order.getId());
					map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
					if(("01").equals(product.getType())){
						//特价美食
						  //在线支付
							if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
								Template template = templateBiz.getTemplateById(Constant.FOOD_ONLINEPAY_DEFINE);
								if(template != null){
										String content = getSmsContent(template.getSource(),map);
										////发送短信
									boolean send = smsBiz.send(content, order.getPhone());
										if(!send){
											throw new Exception("send sms failure!");
										}
								}
							//到付
							}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
								
								Template template = templateBiz.getTemplateById(Constant.FOOD_TOPAY_DEFINE);
								if(template != null){
										String content = getSmsContent(template.getSource(),map);
										//发送短信
									boolean send = smsBiz.send(content, order.getPhone());
										if(!send){
											throw new Exception("send sms failure!");
										}
								}
								
							}
					}else if(("02").equals(product.getType())){
						//旅游商品
						//在线支付
						if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
							Template template = templateBiz.getTemplateById(Constant.TRIP_ONLINEPAY_DEFINE);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									////发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
						//到付
						}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
							
							Template template = templateBiz.getTemplateById(Constant.TRIP_TOPAY_DEFINE);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
							
						}
						
					}else if(("03").equals(product.getType())){
						//线路推荐
						//在线支付
						if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
							Template template = templateBiz.getTemplateById(Constant.LINE_ONLINEPAY_DEFINE);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									////发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
						//到付
						}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
							Template template = templateBiz.getTemplateById(Constant.LINE_TOPAY_DEFINE);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
						}
					}
				}
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
			return true;
		}
		return false;
	}
	
	
	//取消订单（短信）
	@Override  
	public boolean cancelOrder(String orderId)throws AppBusinessException {
		Order order =getOrderDtlById(orderId);
		if(order != null){
			order.setId(orderId);
			order.setStatus(OrderStatus.CANCEL.getValue());
			String stampNum = WebUtil.randTicketNum(new String[]{});
			order.setStampNum(stampNum);
			try{  
				orderDao.updateOrder(order);
				//门票
				if(ProductType.TICKET.getValue().equals(order.getType())){
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("orderId", order.getId());
					map.put("orderTime", Common.getFormatDate(Common.parseDate(order.getOrderTime(), "yyyy-MM-dd"), "MM/dd"));
					map.put("pname", order.getPname());
					map.put("orderRandom", stampNum);
					Ticket ticket = getTicketByOrder(order.getId());
					if(ticket != null){
						map.put("address", ticket.getAddress());
					}
					map.put("contactor", order.getContactor());
					map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
					//线上支付
					if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
						//取消短信
						Template template = templateBiz.getTemplateById(Constant.SCENIC_ONLINEPAY_CANCEL);
						if(template != null){
							String content = getSmsContent(template.getSource(),map);
							if(!ProductType.PRODUCT.getValue().equals(order.getType())){
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						}
						//到付
					}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
						//取消短信
						Template template = templateBiz.getTemplateById(Constant.SCENIC_TOPAY_CANCEL);
						if(template != null){
							String content = getSmsContent(template.getSource(),map);
							if(!ProductType.PRODUCT.getValue().equals(order.getType())){
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						}
					}
					//酒店
				}else if(ProductType.HOTEL.getValue().equals(order.getType())){
					Map<String,Object> map = new HashMap<String,Object>();
					map.put("orderId", order.getId());
					map.put("orderTime", Common.getFormatDate(Common.parseDate(order.getOrderTime(), "yyyy-MM-dd"), "MM/dd"));
					map.put("orderCount", order.getOpList().get(0).getAmount());
					map.put("price", order.getOpList().get(0).getMprice());
					Room room = hotelRoomBiz.getRoomById(order.getOpList().get(0).getPid());
					if(room != null){
						map.put("pname", room.getName());
						map.put("pdates", Common.daysOfTwo(Common.parseDate(order.getOpList().get(0).getSdate(), "yyyy-MM-dd"), Common.parseDate(order.getOpList().get(0).getLdate(), "yyyy-MM-dd")));
						Hotel hotel = hotelRoomBiz.getHotelById(room.getHotel());
						if(hotel != null){
							map.put("address", hotel.getAddress());
							map.put("phone", hotel.getPhone());
							map.put("hotelname", hotel.getName());
						}
					}
						map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
					//线上支付
					if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
						
						Template template = templateBiz.getTemplateById(Constant.HOTEL_ONLINEPAY_CANCEL);
						if(template != null){
							
								String content = getSmsContent(template.getSource(),map);
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						
						//到付
					}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
						Template template = templateBiz.getTemplateById(Constant.HOTEL_TOPAY_CANCEL);
						if(template != null){
								String content = getSmsContent(template.getSource(),map);
								//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
								if(!send){
									throw new Exception("send sms failure!");
								}
							}
						}
					//产品
				}else if(ProductType.PRODUCT.getValue().equals(order.getType())){
					OrderProduct orderproduct =getOpList(order.getId()).get(0);
					Product product =productBiz.getProductById(orderproduct.getPid());
					Map<String,Object> map = new HashMap<String,Object>();
					if(product != null){
						map.put("address", product.getAddress());
						map.put("pname", product.getName());
						}
					map.put("orderId", order.getId());
					map.put("servicePhone", configBiz.getConfigById("servicePhone").getValue());
					if(("01").equals(product.getType())){
						//特价美食
						  //在线支付
							if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
								Template template = templateBiz.getTemplateById(Constant.FOOD_ONLINEPAY_CANCEL);
								if(template != null){
										String content = getSmsContent(template.getSource(),map);
										////发送短信
									boolean send = smsBiz.send(content, order.getPhone());
										if(!send){
											throw new Exception("send sms failure!");
										}
								}
							//到付
							}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
								
								Template template = templateBiz.getTemplateById(Constant.FOOD_TOPAY_CANCEL);
								if(template != null){
										String content = getSmsContent(template.getSource(),map);
										//发送短信
									boolean send = smsBiz.send(content, order.getPhone());
										if(!send){
											throw new Exception("send sms failure!");
										}
								}
								
							}
					}else if(("02").equals(product.getType())){
						//旅游商品
						//在线支付
						if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
							Template template = templateBiz.getTemplateById(Constant.TRIP_ONLINEPAY_CANCEL);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									////发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
						//到付
						}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
							
							Template template = templateBiz.getTemplateById(Constant.TRIP_TOPAY_CANCEL);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
							
						}
						
					}else if(("03").equals(product.getType())){
						//线路推荐
						//在线支付
						if(OrderPayFrom.INTERNET.getValue().equals(order.getPayFrom())){
							Template template = templateBiz.getTemplateById(Constant.LINE_ONLINEPAY_CANCEL);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									////发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
						//到付
						}else if(OrderPayFrom.TOPAYED.getValue().equals(order.getPayFrom())){
							Template template = templateBiz.getTemplateById(Constant.LINE_TOPAY_CANCEL);
							if(template != null){
									String content = getSmsContent(template.getSource(),map);
									//发送短信
								boolean send = smsBiz.send(content, order.getPhone());
									if(!send){
										throw new Exception("send sms failure!");
									}
							}
						}
					}
				}
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
			return true;
		}
		return false;
	}
	
	@Override
	public void islockorder(Order order) throws AppBusinessException {
		orderDao.updateOrderLock(order);
	}
}
